var $id = document.getElementById("checked");
var button = document.getElementById("submit");

button.onclick = function() {
    if ($id.checked === false) {
        alert("请勾选认证协议")
    }

}

// 图像
function preview(obj) {
    var file = document.getElementById('file').value;
    var index = file.lastIndexOf('.');
    var extension = file.substr(index + 1);
    var arr = ['jpeg', 'png', 'jpg', 'gif'];
    if (isInArray(arr, extension)) {
        var img = document.getElementById("previewimg");
        img.src = window.URL.createObjectURL(obj.files[0]);
        var file = obj.files[0];
        UpladFile(file);
    } else {
        alert('请选择正确的图片格式');
        return false;
    }
}


function UpladFile(fileObj) {
    var form = new FormData();
    form.append("file", fileObj);
    xhr = new XMLHttpRequest();
    xhr.open("post", "http://192.168.8.109/home/login/testUpFile", true);
    xhr.send(form);
    xhr.onreadystatechange = function() {
        var data = xhr.response;
        console.log(JSON.parse(data).data.file);
        document.getElementById('callback').innerHTML = JSON.parse(data).data.file
    }
}



function isInArray(arr, value) {
    for (var i = 0; i < arr.length; i++) {
        if (value === arr[i]) {
            return true;
        }
    }
    return false;
}
//营业执照

function preview1(obj) {
    var file1 = document.getElementById('file1').value;
    var index = file1.lastIndexOf('.');
    var extension = file1.substr(index + 1);
    var arr = ['jpeg', 'png', 'jpg', 'gif'];
    if (isInArray1(arr, extension)) {
        var img = document.getElementById("previewimg1");
        img.src = window.URL.createObjectURL(obj.files[0]);
        var file1 = obj.files[0];
        UpladFile1(file1);
    } else {
        alert('请选择正确的图片格式');
        return false;
    }
}

function UpladFile1(fileObj) {
    var form = new FormData();
    form.append("file", fileObj);
    xhr = new XMLHttpRequest();
    xhr.open("post", "http://192.168.8.109/home/login/testUpFile", true);
    xhr.send(form);
    xhr.onreadystatechange = function() {
        var data = xhr.response;
        console.log(JSON.parse(data).data.file);
        document.getElementById('callback1').innerHTML = JSON.parse(data).data.file
    }
}

function isInArray1(arr, value) {
    for (var i = 0; i < arr.length; i++) {
        if (value === arr[i]) {
            return true;
        }
    }
    return false;
}

$(document).ready(function() {
    $("button").click(function() {

        if ($id.checked === true) {
            var phone = document.getElementById('phone').value
            var enterprise = document.getElementById('enterprise').value
            var callback = document.getElementById('callback')
            var file = callback.innerHTML
            var callback1 = document.getElementById('callback1')
            var file1 = callback1.innerHTML

            $.ajax({
                url: 'http://192.168.8.109/api/login/enterprise',
                type: 'post',
                data: {
                    phone: phone,
                    enter_name: enterprise,
                    head_pic: file,
                    license: file1
                },
                success: function(res) {
                    console.log(res);
                    var status = JSON.parse(res).status
                    showTips(JSON.parse(res).msg, 700, 3);

                    function showTips(content, height, time) {
                        //窗口的宽度  
                        var windowWidth = $(window).width();
                        var tipsDiv = '<div class="tipsClass">' + content + '</div>';

                        $('body').append(tipsDiv);
                        $('div.tipsClass').css({
                            'position': 'absolute',
                            'top': 50 + '%',
                            'left': 50 + '%',
                            'padding': '3px 5px',
                            'background': 'rgba(51, 51, 51, 0.4)',
                            'font-size': 18 + 'px',
                            'margin-left': -50 + 'px',
                            'border-radius': 10 + 'px',
                            'margin-top': -25 + 'px',
                            'line-height': '50px',
                            'text-align': 'center',
                            'width': 'auto',
                            'height': '50px',
                            'color': '#fff',
                            'opacity': '1'
                        }).show();
                        clearTimeout(settimer)
                        var settimer = setTimeout(function() { $('div.tipsClass').remove(); }, (time * 500));
                        content = ''
                        if (status === 1) {
                            setTimeout(function() {
                                window.location = 'companyLogin.html'
                            }, 2000)
                        }
                    }
                },

            })
        }
    });
});